package com.wintrisstech.iaroc.irobot;

import java.io.IOException;

import com.sun.spot.util.Utils;

public abstract class IRobotCreateSerialConnection {
    protected int[] recordedBytes;
    protected int recordedBytesCount;
    protected boolean isRecording;

    public IRobotCreateSerialConnection() {
        this.recordedBytes = new int[IRobotCreateConstants.MAX_COMMAND_SIZE];
        this.recordedBytesCount = 0;
        isRecording = false;
    }
    
    public abstract void flushRead();
    
    public int[] getRecordedBytes() {
        return recordedBytes;
    }
    
    public int getRecordedBytesCount() {
        return recordedBytesCount;
    }
    
    public boolean isRecording() {
        return isRecording;
    }

    public abstract int readSignedByte() throws IOException;

    public abstract int readUnsignedByte() throws IOException;

    public int readSignedWord() throws IOException {
        // Java is already twos complement, so no need for any translation
        int signed = ((readSignedByte() << 8) & 0xFFFFFF00) | readSignedByte();
        return signed;
    }
    
    public abstract int readUnsignedBytes(int[] buffer, int start, int length) throws IOException;

    public int readUnsignedWord() throws IOException {
        int unsigned = (readUnsignedByte() << 8) | readUnsignedByte();
        return unsigned;
    }
    
    public void sendByte(int b) throws IOException {
        if (isRecording) {
            recordedBytes[recordedBytesCount++] = b;
        } else {
            sendBytePrim(b);
        }
    }
    
    public abstract void sendBytePrim(int b) throws IOException;
    
    public void sendBytes(int[] bytes, int start, int length) throws IOException {
        if (isRecording) {
            for (; length > 0; start++, length--) {
                recordedBytes[recordedBytesCount++] = bytes[start];
            }
        } else {
            sendBytesPrim(bytes, start, length);
        }
    }
    

    public abstract void sendBytesPrim(int[] bytes, int start, int length) throws IOException;

    public void sendSignedWord(int value) throws IOException {
        // Java bit representation is already two's complement
        sendByte(value >> 8);
        sendByte(value & 0xFF);
    }
    
    public void sendUnsignedWord(int value) throws IOException {
        sendByte(value >> 8);
        sendByte(value & 0xFF);
    }
    
    public void afterCommandPause() {
        if(isRecording) {
            return;
        }
        if (IRobotCreateConstants.DEBUG_SERIAL) {
            System.out.println("afterCommandPause");
        }
        Utils.sleep(IRobotCreateConstants.AFTER_COMMAND_PAUSE_TIME);
    }

    public void sleep(int ms) {
        if(isRecording) {
            return;
        }
        if (IRobotCreateConstants.DEBUG_SERIAL) {
            System.out.print("sleep: ");
            System.out.println(ms);
        }
        Utils.sleep(ms);
    }

    public void startRecording() {
        isRecording = true;
    }
    
    public int stopRecording() {
        isRecording = false;
        int count = recordedBytesCount;
        recordedBytesCount = 0;
        return count;
    }
    
}
